var express = require('express');
var router = express.Router();
var process = require('child_process');
var os = require('os');
var http = require('http');
var config = require('../routes/config');

var fs = require("fs");

/* GET home page. */
router.get('/', function(req, res, next) {
	var data = [];
	if (req.session.login)
    {
        if (req.session.ismanager) {
            config.selectAllGoodsSqlite3(function(objects){
                var heads=config.option_heads;
                var datas=[];
                for(var i = 0; i < objects.length; ++i) {
                    var object = objects[i];
                    var arr = [];
                    arr.push({value:object.UNIQUE_TAG});
                    arr.push({value:object.SHIYONGCHEXING});
                    arr.push({value:object.PEIJIANMINGCHENG});
                    arr.push({value:object.CHANDI});
                    arr.push({value:object.KUNCUNZHUANGTAI});
                    arr.push({value:object.KUNCUNLIANG});
                    arr.push({value:object.YOUHUIDANWEI});
                    arr.push({value:object.JINJIA});
                    arr.push({value:object.LINGJIANHAO});
                    arr.push({value:object.WULIAOHAO});
                    arr.push({value:object.TIAOMAHAO});
                    arr.push({value:object.JINHUOSHIJIAN});
                    datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
                }
        
                res.render('goods_manager',{
                    links:config.linkmenu,
                    index:config.link.INDEX,
                    login:req.session.login,
                    datas:datas,
                    heads:heads,
                    ismanager:req.session.ismanager
                });
            });
        }
        else
        {
            config.selectAllGoodsSqlite3(function(objects){
                var heads=config.option_heads;
                var datas=[];
                for(var i = 0; i < objects.length; ++i) {
                    var object = objects[i];
                    var arr = [];
                    arr.push({value:object.UNIQUE_TAG});
                    arr.push({value:object.SHIYONGCHEXING});
                    arr.push({value:object.PEIJIANMINGCHENG});
                    arr.push({value:object.CHANDI});
                    arr.push({value:object.KUNCUNZHUANGTAI});
                    arr.push({value:object.KUNCUNLIANG});
                    arr.push({value:object.YOUHUIDANWEI});
                    arr.push({value:object.JINJIA});
                    arr.push({value:object.LINGJIANHAO});
                    arr.push({value:object.WULIAOHAO});
                    arr.push({value:object.TIAOMAHAO});
                    arr.push({value:object.JINHUOSHIJIAN});
                    datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
                }
        
                res.render('order_manager',{
                    links:config.linkmenu_client,
                    index:config.link.INDEX,
                    login:req.session.login,
                    datas:datas,
                    heads:heads,
                    ismanager:req.session.ismanager
                });
            });
        }
    }
    else
    {
        res.render('login',{
           // links:config.linkmenu,
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }

});

router.get('/login', function(req, res, next) {
    res.render('login',{
        //links:config.linkmenu,
        index:config.link.INDEX.link,
        login:req.session.login
    });
});

router.get('/add_goods',function(req,res,next){
	if (req.session.login)
    {
        if (req.session.ismanager) {
             
            res.render('add_goods', { title: '增加配件' , links:config.linkmenu,index:config.link.INDEX,login:req.session.login});
        }
        else
        {
            req.session.login = false;
            req.session.userinfo = {};
            res.render('login',{
                // links:config.linkmenu,
                 index:config.link.INDEX.link,
                 login:false
             });
        }
    }
    else
    {
        res.render('login',{
           // links:config.linkmenu,
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }
});


router.post("/add_goods",function (req,res,next) {
    
    var object = {};
    object.SHIYONGCHEXING = req.body.SHIYONGCHEXING;
    object.PEIJIANMINGCHENG = req.body.SHIYONGCHEXING;
    object.CHANDI  = req.body.CHANDI;
    object.KUNCUNZHUANGTAI  = req.body.KUNCUNZHUANGTAI;
    object.KUNCUNLIANG  = req.body.KUNCUNLIANG;
    object.YOUHUIDANWEI  = req.body.YOUHUIDANWEI;
    object.JINJIA = req.body.JINJIA;
    object.LINGJIANHAO  = req.body.LINGJIANHAO;
    object.WULIAOHAO  = req.body.WULIAOHAO;
    object.TIAOMAHAO  = req.body.TIAOMAHAO;
    object.JINHUOSHIJIAN  = req.body.JINHUOSHIJIAN;

    var arrFiles = [];
    for (var i = 0; i < req.files.length;++i) {
        console.log(req.files[i]);  // 上传的文件信息

        var des_file = "./public/images/" + req.files[i].originalname;

        fs.renameSync(req.files[i].path,des_file);

        arrFiles.push(req.files[i].originalname);
        /*
        fs.readFile( req.files[i].path, function (err, data) {
            fs.writeFile(des_file, data, function (err) {
            if( err ){
                console.log( err );
            }else{
                response = {
                        message:'File uploaded successfully', 
                        filename:des_file
                };
            }
            console.log( response );
            });
        });*/
    }
    if (req.files.length)
    object.IMAGES = JSON.stringify(arrFiles);

    config.addGoodsSqlite3(object,function(){
        config.selectAllGoodsSqlite3(function(objects){
            var heads=config.option_heads;
            var datas=[];
            for(var i = 0; i < objects.length; ++i) {
                var object = objects[i];
                var arr = [];
                arr.push({value:object.UNIQUE_TAG});
                arr.push({value:object.SHIYONGCHEXING});
                arr.push({value:object.PEIJIANMINGCHENG});
                arr.push({value:object.CHANDI});
                arr.push({value:object.KUNCUNZHUANGTAI});
                arr.push({value:object.KUNCUNLIANG});
                arr.push({value:object.YOUHUIDANWEI});
                arr.push({value:object.JINJIA});
                arr.push({value:object.LINGJIANHAO});
                arr.push({value:object.WULIAOHAO});
                arr.push({value:object.TIAOMAHAO});
                arr.push({value:object.JINHUOSHIJIAN});
                datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
            }
    
            res.render('goods_manager',{
                links:config.linkmenu,
                index:config.link.INDEX,
                login:req.session.login,
                datas:datas,
                heads:heads,
                ismanager:req.session.ismanager
            });
        });
    });
    console.log("订单信息 " + JSON.stringify(object));
});

router.post("/sumbit_edit_goods",function (req,res,next) {
    
    var object = {};
    object.SHIYONGCHEXING = req.body.SHIYONGCHEXING;
    object.PEIJIANMINGCHENG = req.body.PEIJIANMINGCHENG;
    object.CHANDI  = req.body.CHANDI;
    object.KUNCUNZHUANGTAI  = req.body.KUNCUNZHUANGTAI;
    object.KUNCUNLIANG  = req.body.KUNCUNLIANG;
    object.YOUHUIDANWEI  = req.body.YOUHUIDANWEI;
    object.JINJIA = req.body.JINJIA;
    object.LINGJIANHAO  = req.body.LINGJIANHAO;
    object.WULIAOHAO  = req.body.WULIAOHAO;
    object.TIAOMAHAO  = req.body.TIAOMAHAO;
    object.JINHUOSHIJIAN  = req.body.JINHUOSHIJIAN;
    object.UNIQUE_TAG = req.body.UNIQUE_TAG;
    
    var arrFiles = [];
    for (var i = 0; i < req.files.length;++i) {
        console.log(req.files[i]);  // 上传的文件信息

        var des_file = "./public/images/" + req.files[i].originalname;

        fs.renameSync(req.files[i].path,des_file);

        arrFiles.push(req.files[i].originalname);
        /*
        fs.readFile( req.files[i].path, function (err, data) {
            fs.writeFile(des_file, data, function (err) {
            if( err ){
                console.log( err );
            }else{
                response = {
                        message:'File uploaded successfully', 
                        filename:des_file
                };
            }
            console.log( response );
            });
        });*/
    }
    if (req.files.length)
    object.IMAGES = JSON.stringify(arrFiles);

    console.log("提交更改 " + JSON.stringify(object));
   
    config.updateGoodsSqlite3(object,function(){
        config.selectAllGoodsSqlite3(function(objects){
            var heads=config.option_heads;
            var datas=[];
            for(var i = 0; i < objects.length; ++i) {
                var object = objects[i];
                var arr = [];
                arr.push({value:object.UNIQUE_TAG});
                arr.push({value:object.SHIYONGCHEXING});
                arr.push({value:object.PEIJIANMINGCHENG});
                arr.push({value:object.CHANDI});
                arr.push({value:object.KUNCUNZHUANGTAI});
                arr.push({value:object.KUNCUNLIANG});
                arr.push({value:object.YOUHUIDANWEI});
                arr.push({value:object.JINJIA});
                arr.push({value:object.LINGJIANHAO});
                arr.push({value:object.WULIAOHAO});
                arr.push({value:object.TIAOMAHAO});
                arr.push({value:object.JINHUOSHIJIAN});
                datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
            }
    
            res.render('goods_manager',{
                links:config.linkmenu,
                index:config.link.INDEX,
                login:req.session.login,
                datas:datas,
                heads:heads,
                ismanager:req.session.ismanager
            });
        });
    });
    console.log("订单信息 " + JSON.stringify(object));
});


router.get('/logout', function(req, res, next) {
    req.session.login = false;
    req.session.userinfo = {};
    res.render('index',{
        //links:config.linkmenu,
        index:config.link.INDEX.link,
        login:req.session.login
    });
});

router.post("/login",function (req,res,next) {
    console.log(req.body.USERNAME + " " + req.body.PASSWD);

    config.checkUser("LOGINUSER_DATA","EMAIL='" + new Buffer(req.body.USERNAME).toString('base64')+"' AND STATE=1 AND PASSWD='" +new Buffer(req.body.PASSWD).toString('base64')+ "'",function(result) {
        if (!result || result.length !=1) {
            res.render('error', {
                links: config.linkmenu,
                index: config.link.INDEX.link,
                login: req.session.login,
                message: "密码或者用户名错误或者尚未激活",
            });
        }
        else
        {
            req.session.login = true;
            var o = {};
            o.name = req.body.USERNAME;
            o.passwd = req.body.PASSWD;
            req.session.userinfo=o;
            res.render('index',{
                links:config.linkmenu,
                index:config.link.INDEX,
                login:req.session.login
            });
        }

    });
});
router.post("/edit_goods",function (req,res,next) {
    if (!req.session.login)
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
    console.log(JSON.stringify(req.body));
    var unqiue_tag = req.body.unqiue_tag;
    console.log("编辑条目 " + unqiue_tag);
    var condtion = "UNIQUE_TAG="+unqiue_tag+"";
    config.selectGoodsSqlite3(condtion,function(objects){
        if (objects.length)
        {
            var object = objects[0];
            res.render('edit_goods',{
                links:config.linkmenu,
                index:config.link.INDEX,
                login:req.session.login,
                object:object,
                ismanager:req.session.ismanager
            });
            console.log("开始编辑条目 " + JSON.stringify(object));
        }
    });
});

router.post("/buy_goods",function (req,res,next) {
    if (!req.session.login)
    {
        res.render('index',{
            index:config.link.INDEX
        });
        return;
    }
    console.log(JSON.stringify(req.body));
    var unqiue_tag = req.body.unqiue_tag;
    var condtion = "UNIQUE_TAG="+unqiue_tag+"";
    config.selectGoodsSqlite3(condtion,function(objects){
        if (objects.length)
        {
             var object = objects[0];
             var o = req.session.userinfo;
             var data = {};
             data.USERNAME = o.name;
             data.GOODS_ID = unqiue_tag;
             data.BUY_TIME = new Date();
             data.BUY_NUMBER = 1;
             data.ORDER_STATE = 1;
             console.log("购买条目 " + JSON.stringify(data));
             config.addOrderSqlite3(data,function(){});
             res.send({status:"ok"});
        }
    });
});

router.post("/look_goods_image",function (req,res,next) {
    if (!req.session.login )
    {
        res.render('index',{
            links:config.linkmenu,
            index:config.link.INDEX
        });
        return;
    }
    
    console.log(JSON.stringify(req.body));
    var unqiue_tag = req.body.unqiue_tag;
    if (unqiue_tag == "")
    {
        return;
    }
    console.log("编辑条目 " + unqiue_tag);
    var condtion = "UNIQUE_TAG="+unqiue_tag+"";

    var links = null;
    if (!req.session.ismanager) links = config.linkmenu_client;
    else links = config.linkmenu;
    config.selectGoodsSqlite3(condtion,function(objects){
        if (objects.length)
        {
            var object = objects[0];
            try{
                var images = JSON.parse(object.IMAGES);
                res.render('look_goods_image',{
                    links:links,
                    index:config.link.INDEX,
                    login:req.session.login,
                    object:object,
                    images:images,
                    ismanager:req.session.ismanager
                });
            }catch(e){
                res.render('look_goods_image',{
                    links:links,
                    index:config.link.INDEX,
                    login:req.session.login,
                    object:object,
                    ismanager:req.session.ismanager
                });
            }
            console.log("查看 " + JSON.stringify(object));
        }
    });
});


router.post("/search_goods",function (req,res,next) {
    console.log("查找配件 " + req.body.category + " " + req.body.keyword);
    var TABLE_COLUMN_NAME = "";
    if (req.body.category  == "适用车型") TABLE_COLUMN_NAME = "SHIYONGCHEXING";
    if (req.body.category  == "配件名称") TABLE_COLUMN_NAME = "PEIJIANMINGCHENG";
    if (req.body.category  == "产地") TABLE_COLUMN_NAME = "CHANDI";
    if (req.body.category  == "库存状态") TABLE_COLUMN_NAME = "KUNCUNZHUANGTAI";
    if (req.body.category  == "库存量") TABLE_COLUMN_NAME = "KUNCUNLIANG";
    if (req.body.category  == "有优惠单位") TABLE_COLUMN_NAME = "YOUHUIDANWEI";
    if (req.body.category  == "进价") TABLE_COLUMN_NAME = "JINJIA";
    if (req.body.category  == "零件号") TABLE_COLUMN_NAME = "LINGJIANHAO";
    if (req.body.category  == "物料号") TABLE_COLUMN_NAME = "WULIAOHAO";
    if (req.body.category  == "条码号") TABLE_COLUMN_NAME = "TIAOMAHAO";
    if (req.body.category  == "上次进货日期") TABLE_COLUMN_NAME = "JINHUOSHIJIAN";
    var condtion = TABLE_COLUMN_NAME + " like '%"+req.body.keyword+"%'";
    console.log("查询条件 " +condtion);
    config.selectGoodsSqlite3(condtion,function(objects){
        var heads=config.option_heads;
        var datas=[];
        for(var i = 0; i < objects.length; ++i) {
            var object = objects[i];
            var arr = [];
            arr.push({value:object.UNIQUE_TAG});
            arr.push({value:object.SHIYONGCHEXING});
            arr.push({value:object.PEIJIANMINGCHENG});
            arr.push({value:object.CHANDI});
            arr.push({value:object.KUNCUNZHUANGTAI});
            arr.push({value:object.KUNCUNLIANG});
            arr.push({value:object.YOUHUIDANWEI});
            arr.push({value:object.JINJIA});
            arr.push({value:object.LINGJIANHAO});
            arr.push({value:object.WULIAOHAO});
            arr.push({value:object.TIAOMAHAO});
            arr.push({value:object.JINHUOSHIJIAN});
            datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
        }
        if (req.session.ismanager)
        res.render('goods_manager',{
            links:config.linkmenu,
            index:config.link.INDEX,
            login:req.session.login,
            datas:datas,
            heads:heads,
            ismanager:req.session.ismanager
        });
        else
        res.render('order_manager',{
            links:config.linkmenu,
            index:config.link.INDEX,
            login:req.session.login,
            datas:datas,
            heads:heads,
            ismanager:req.session.ismanager
        });
    });
});
router.post("/loginv1",function (req,res,next) {
    console.log("登陆信息 " + req.body.USERNAME + " " + req.body.PASSWD);

    config.checkUserAndPasswdSqlite3(req.body.USERNAME,req.body.PASSWD,function(result) {
        if (!result) {
            res.render('error', {
                links: config.linkmenu,
                index: config.link.INDEX.link,
                login: req.session.login,
                message: "密码或者用户名错误或者尚未激活",
            });
        }
        else
        {
            req.session.login = true;
            var o = {};
            o.name = req.body.USERNAME;
            o.passwd = req.body.PASSWD;
            req.session.userinfo=o;
            if (o.name == "manager")
                req.session.ismanager = true;
            else 
                req.session.ismanager = false;
            config.selectAllGoodsSqlite3(function(objects){
                var heads=config.option_heads;
                var datas=[];
                for(var i = 0; i < objects.length; ++i) {
                    var object = objects[i];
                    var arr = [];
                    arr.push({value:object.UNIQUE_TAG});
                    arr.push({value:object.SHIYONGCHEXING});
                    arr.push({value:object.PEIJIANMINGCHENG});
                    arr.push({value:object.CHANDI});
                    arr.push({value:object.KUNCUNZHUANGTAI});
                    arr.push({value:object.KUNCUNLIANG});
                    arr.push({value:object.YOUHUIDANWEI});
                    arr.push({value:object.JINJIA});
                    arr.push({value:object.LINGJIANHAO});
                    arr.push({value:object.WULIAOHAO});
                    arr.push({value:object.TIAOMAHAO});
                    arr.push({value:object.JINHUOSHIJIAN});
                    datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
                }
                if (req.session.ismanager)
                {
                    res.render('goods_manager',{
                        links:config.linkmenu,
                        index:config.link.INDEX,
                        login:req.session.login,
                        datas:datas,
                        heads:heads,
                        ismanager:req.session.ismanager
                    });
                }
                else
                {
                    res.render('order_manager',{
                        links:config.linkmenu_client,
                        index:config.link.INDEX,
                        login:req.session.login,
                        datas:datas,
                        heads:heads,
                        ismanager:req.session.ismanager
                    });
                }
            });
        }

    });
});

router.get("/userprofile",function (req,res,next) {
    var o = req.session.userinfo;
    if (req.session.login)
    {
        res.render('userprofile',{
            links:config.linkmenu,
            index:config.link.INDEX,
            login:req.session.login,
            user:o
        });
    }
    else
    {
        res.render('login',{
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }
})

router.get("/goods_manager",function (req,res,next) {
    var o = req.session.userinfo;
    if (req.session.login && req.session.ismanager)
    {
        config.selectAllGoodsSqlite3(function(objects){
            var heads=config.option_heads;
            var datas=[];
            for(var i = 0; i < objects.length; ++i) {
                var object = objects[i];
                var arr = [];
                arr.push({value:object.UNIQUE_TAG});
                arr.push({value:object.SHIYONGCHEXING});
                arr.push({value:object.PEIJIANMINGCHENG});
                arr.push({value:object.CHANDI});
                arr.push({value:object.KUNCUNZHUANGTAI});
                arr.push({value:object.KUNCUNLIANG});
                arr.push({value:object.YOUHUIDANWEI});
                arr.push({value:object.JINJIA});
                arr.push({value:object.LINGJIANHAO});
                arr.push({value:object.WULIAOHAO});
                arr.push({value:object.TIAOMAHAO});
                arr.push({value:object.JINHUOSHIJIAN});
                console.log("订单信息" + JSON.stringify(object));
                datas.push({values:arr,unqiue_tag:object.UNIQUE_TAG});
            }
    
            res.render('goods_manager',{
                links:config.linkmenu,
                index:config.link.INDEX,
                login:req.session.login,
                datas:datas,
                heads:heads,
                ismanager:req.session.ismanager
            });
        });
    }
    else
    {
        res.render('login',{
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }
})

function deepClone(obj){
    let _obj = JSON.stringify(obj),
        objClone = JSON.parse(_obj);
    return objClone
} 

router.post("/add_buynumber",function(req,res,next){
    var o = req.session.userinfo;

    console.log(JSON.stringify(req.body));
    var unqiue_tag = req.body.unqiue_tag;
    if (req.session.login)
    {
        config.selectOrderByID(unqiue_tag,function(orrr){
            if (orrr){
                var number = orrr.BUY_NUMBER + 1;
                orrr.BUY_NUMBER = number;
                console.log("更新购物车 " + JSON.stringify(orrr));
                config.updateOrderNumberSqlite3(unqiue_tag,orrr,function(){
                    config.selectOrderByUserNameSqite3(o.name,function(orders){
                        config.selectAllGoodsSqlite3(function(objects){
                            var heads=deepClone(config.option_heads_client);
                            var datas=[];
                            var goods_index={}
                            for(var i = 0; i < objects.length; ++i) {
                                var object = objects[i];
                            
                                goods_index[object.UNIQUE_TAG] = object;
                            }
                            for (var i = 0; i < orders.length;++i) {
                                var order = orders[i];
                                if (order.ORDER_STATE == 1)
                                {
                                    var arr = [];
                                    var object = goods_index[order.GOODS_ID];
                                    arr.push({value:object.UNIQUE_TAG});
                                    arr.push({value:object.SHIYONGCHEXING});
                                    arr.push({value:object.PEIJIANMINGCHENG});
                                    arr.push({value:object.CHANDI});
                                    arr.push({value:object.KUNCUNZHUANGTAI});
                                    arr.push({value:object.KUNCUNLIANG});
                                    arr.push({value:object.YOUHUIDANWEI});
                                    arr.push({value:object.JINJIA});
                                    arr.push({value:object.LINGJIANHAO});
                                    arr.push({value:object.WULIAOHAO});
                                    arr.push({value:object.TIAOMAHAO});
                                    arr.push({value:object.JINHUOSHIJIAN});
                                    arr.push({value:order.BUY_NUMBER});
                                    console.log("订单信息" + JSON.stringify(object));
                                    datas.push({values:arr,unqiue_tag:order.ORDER_ID,goods_unique_tag:object.UNIQUE_TAG});
                                }    
                                console.log("订单状态 " + JSON.stringify(order));
                            }
                            res.render('shop',{
                                links:config.linkmenu_client,
                                index:config.link.INDEX,
                                login:req.session.login,
                                datas:datas,
                                heads:heads,
                                ismanager:req.session.ismanager
                            });
                        });
                    });
                })
            }
        })
        
        
    }
    else
    {
        res.render('login',{
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }
})

router.post("/minus_buynumber",function(req,res,next){
    var o = req.session.userinfo;

    console.log(JSON.stringify(req.body));
    var unqiue_tag = req.body.unqiue_tag;
    if (req.session.login)
    {
        config.selectOrderByID(unqiue_tag,function(orrr){
            if (orrr){
                var number = orrr.BUY_NUMBER - 1;
                var func = null;
                if (number <= 0){
                    func = config.deleteOrderSqlite3;
                }
                else {
                    func = config.updateOrderNumberSqlite3;
                }
                orrr.BUY_NUMBER = number;
                console.log("更新购物车 " + JSON.stringify(orrr));
                func(unqiue_tag,orrr,function(){
                    config.selectOrderByUserNameSqite3(o.name,function(orders){
                        config.selectAllGoodsSqlite3(function(objects){
                            var heads=deepClone(config.option_heads_client);
                            var datas=[];
                            var goods_index={}
                            for(var i = 0; i < objects.length; ++i) {
                                var object = objects[i];
                            
                                goods_index[object.UNIQUE_TAG] = object;
                            }
                            for (var i = 0; i < orders.length;++i) {
                                var order = orders[i];
                                if (order.ORDER_STATE == 1)
                                {
                                    var arr = [];
                                    var object = goods_index[order.GOODS_ID];
                                    arr.push({value:object.UNIQUE_TAG});
                                    arr.push({value:object.SHIYONGCHEXING});
                                    arr.push({value:object.PEIJIANMINGCHENG});
                                    arr.push({value:object.CHANDI});
                                    arr.push({value:object.KUNCUNZHUANGTAI});
                                    arr.push({value:object.KUNCUNLIANG});
                                    arr.push({value:object.YOUHUIDANWEI});
                                    arr.push({value:object.JINJIA});
                                    arr.push({value:object.LINGJIANHAO});
                                    arr.push({value:object.WULIAOHAO});
                                    arr.push({value:object.TIAOMAHAO});
                                    arr.push({value:object.JINHUOSHIJIAN});
                                    arr.push({value:order.BUY_NUMBER});
                                    console.log("订单信息" + JSON.stringify(object));
                                    datas.push({values:arr,unqiue_tag:order.ORDER_ID,goods_unique_tag:object.UNIQUE_TAG});
                                }    
                                console.log("订单状态 " + JSON.stringify(order));
                            }
                            res.render('shop',{
                                links:config.linkmenu_client,
                                index:config.link.INDEX,
                                login:req.session.login,
                                datas:datas,
                                heads:heads,
                                ismanager:req.session.ismanager
                            });
                        });
                    });
                })
            }
            
        })
        
        
    }
    else
    {
        res.render('login',{
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }
})

router.get("/shop",function (req,res,next) {
    var o = req.session.userinfo;
    if (req.session.login)
    {
        config.selectOrderByUserNameSqite3(o.name,function(orders){
            config.selectAllGoodsSqlite3(function(objects){
                var heads=deepClone(config.option_heads_client);
                var datas=[];
                var goods_index={}
                for(var i = 0; i < objects.length; ++i) {
                    var object = objects[i];
                  
                    goods_index[object.UNIQUE_TAG] = object;
                }
                for (var i = 0; i < orders.length;++i) {
                    var order = orders[i];
                    if (order.ORDER_STATE == 1)
                    {
                        var arr = [];
                        var object = goods_index[order.GOODS_ID];
                        arr.push({value:object.UNIQUE_TAG});
                        arr.push({value:object.SHIYONGCHEXING});
                        arr.push({value:object.PEIJIANMINGCHENG});
                        arr.push({value:object.CHANDI});
                        arr.push({value:object.KUNCUNZHUANGTAI});
                        arr.push({value:object.KUNCUNLIANG});
                        arr.push({value:object.YOUHUIDANWEI});
                        arr.push({value:object.JINJIA});
                        arr.push({value:object.LINGJIANHAO});
                        arr.push({value:object.WULIAOHAO});
                        arr.push({value:object.TIAOMAHAO});
                        arr.push({value:object.JINHUOSHIJIAN});
                        arr.push({value:order.BUY_NUMBER});
                        console.log("订单信息" + JSON.stringify(object));
                        datas.push({values:arr,unqiue_tag:order.ORDER_ID,goods_unique_tag:object.UNIQUE_TAG});
                    }    
                    console.log("订单状态 " + JSON.stringify(order));
                }
                res.render('shop',{
                    links:config.linkmenu_client,
                    index:config.link.INDEX,
                    login:req.session.login,
                    datas:datas,
                    heads:heads,
                    ismanager:req.session.ismanager
                });
            });
        });
        
    }
    else
    {
        res.render('login',{
            index:config.link.INDEX.link,
            login:req.session.login
        });
    }
})

module.exports = router;
